package com.sunshine.homepage.controller;

import com.sunshine.homepage.entity.Products;
import com.sunshine.homepage.service.MerchantUpdateService;

import com.sunshine.util.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/merchant")
public class HomepageMerchantController {
    private static final Logger log = LoggerFactory.getLogger(HomepageMerchantController.class);

    @Resource
    private MerchantUpdateService merchantUpdateService;

    /**
     * 添加商品信息
     * 如果需要上传商品图片，请在商品信息中包含图片数据
     *
     * @param products 商品信息
     * @param request HTTP请求
     * @return 是否添加成功
     */
/*    @PostMapping("/addProducts")
    public Result<Boolean> addProducts(@RequestBody Products products, HttpServletRequest request) {
        log.info("添加商品: {}", products);
        return merchantUpdateService.addProducts(products);
    }*/
    
    /**
     * 添加商品信息（带图片上传）
     *
     * @param productName 商品名称
     * @param category 商品分类
     * @param price 商品价格
     * @param stock 库存数量
     * @param status 商品状态
     * @param imageFile 主图文件
     * @param imageFiles 详情图文件列表
     * @param request HTTP请求
     * @return 是否添加成功
     */
    @PostMapping(value = "/addProductsWithImages", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> addProductsWithImages(
            @RequestParam(value = "productName", required = true) String productName,
            @RequestParam(value = "category", required = true) String category,
            @RequestParam(value = "price", required = true) Double price,
            @RequestParam(value = "stock", required = true) Integer stock,
            @RequestParam(value = "status", required = true) Integer status,
            @RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
            @RequestParam(value = "imageFiles", required = false) MultipartFile[] imageFiles,
            HttpServletRequest request) {
        Long userIdFromRequest = TokenUtil.getUserIdFromRequest(request);
        
        Products products = new Products();
        products.setMerchantId(userIdFromRequest);
        products.setProductName(productName);
        products.setCategory(category);
        products.setPrice(price);
        products.setStock(stock);
        products.setStatus(status);
        
        log.info("添加商品（带图片）: {}", products);
        return merchantUpdateService.addProductsWithImages(products, imageFile, imageFiles);
    }

    @PostMapping("/deleteProducts")
    public Result<Boolean> deleteProducts(@RequestBody Products products, HttpServletRequest request) {
        Long userIdFromRequest = TokenUtil.getUserIdFromRequest(request);
        products.setMerchantId(userIdFromRequest);
        log.info("删除商品: {}", products);
        return merchantUpdateService.deleteProducts(products);
    }

    /**
     * 分页查询商家商品列表
     * 根据商家ID分页查询其商品信息列表
     *
     * @param page 页码，从0开始，默认为0
     * @param size 每页大小，默认为7
     * @param request HTTP请求对象，用于获取当前用户信息
     * @return 商品信息列表的分页结果
     */
    @PostMapping("/findProducts")
    public Result<List<Products>>findProducts(@RequestParam(defaultValue = "0") int page,
                                       @RequestParam(defaultValue = "7") int size,
                                       HttpServletRequest request) {
        Long userIdFromRequest = TokenUtil.getUserIdFromRequest(request);
        
        // 构造Products对象用于查询
        Products products = new Products();
        products.setMerchantId(userIdFromRequest);
        
        log.info("查询商品: 商家ID = {}, page = {}, size = {}", userIdFromRequest, page, size);
        Result<List<Products>> productsResult = merchantUpdateService.findProducts(products, page, size);
        log.info("查询结果: {}", productsResult);
        return productsResult;

    }

    /**
     * 更新商品信息
     * 根据请求中的商品信息更新对应商品的数据
     *
     * @param products 商品信息对象，包含需要更新的商品数据
     * @param request HTTP请求对象，用于获取当前用户信息
     * @return 更新结果，成功返回true，失败返回false
     */
    @PostMapping("/updateProducts")
    public Result<Boolean> updateProducts(@RequestBody Products products, HttpServletRequest request) {
        Long userIdFromRequest = TokenUtil.getUserIdFromRequest(request);
        products.setMerchantId(userIdFromRequest);
        log.info("更新商品: {}", products);
        return merchantUpdateService.updateProducts(products);
    }

    /**
     * 根据商品名称查询商品ID
     * 通过商品名称和商家ID查询对应商品的ID
     *
     * @param productName 商品名称
     * @param request HTTP请求对象，用于获取当前用户信息
     * @return 商品ID的查询结果
     */
    @PostMapping("/findProductIdByName")
    public Result<Integer> findProductIdByName(@RequestParam String productName,HttpServletRequest request) {
        Long userIdFromRequest = TokenUtil.getUserIdFromRequest(request);
        log.info("根据商品名称查询商品ID: {} {}", productName , userIdFromRequest);
        return merchantUpdateService.findProductIdByName(productName, userIdFromRequest);
    }
}